package com.xingkeduo.business.order.service;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.dto.transter.activitycardlist.ActivityCardList;
import com.xingkeduo.dto.transter.activitycardlist.ActivityCardListParameterDto;
import com.xingkeduo.dto.transter.activitycardlist.ActivityCardListResponseDto;
import com.xingkeduo.dto.transter.activitycardlist.Activity_Card_list;
import com.xingkeduo.dto.transter.activitycouponlist.ActivityCouponList;
import com.xingkeduo.dto.transter.activitycouponlist.ActivityCouponListParameterDto;
import com.xingkeduo.dto.transter.activitycouponlist.ActivityCouponListResponseDto;
import com.xingkeduo.dto.transter.activitycouponlist.Activity_Coupon_list;
import com.xingkeduo.dto.transter.activitystorelist.ActivityStoreList;
import com.xingkeduo.dto.transter.activitystorelist.ActivityStoreListParameterDto;
import com.xingkeduo.dto.transter.activitystorelist.ActivityStoreListResponseDto;
import com.xingkeduo.dto.transter.activitystorelist.Activity_Store_list;
import com.xingkeduo.remote.feign.MarketingFeignService;
import com.xingkeduo.util.pojo.exception.DataException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ActivityService {

	@Resource
	private MarketingFeignService marketingFeignService;

	public String acitivty_store_list(ActivityStoreListParameterDto activityStoreListParameterDto) {

		String resultJson = marketingFeignService.getUserActivity(activityStoreListParameterDto.covert());
		log.info("acitivty_store_list_resultJson:{}", resultJson);
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultData(jsonData);
		log.info("acitivty_store_list_data:{}", data);
		JSONObject json = JSONObject.parseObject(data);
		ActivityStoreList storeList = new ActivityStoreList();
		// 可用优惠活动
		JSONArray availableActivityList = json.getJSONArray("availableActivityList");
		List<Activity_Store_list> activity_list = availableActivityList.stream().map(this::queryStore_list)
				.collect(Collectors.toList());
		storeList.setActivity_list(activity_list);
		storeList.setActivity_count(0);
		if (!CollectionUtils.isEmpty(activity_list)) {
			storeList.setActivity_count(activity_list.size());
		}
		// 不可用优惠活动
		JSONArray unAvailableActivityList = json.getJSONArray("unavailableActivityList");
		List<Activity_Store_list> un_activity_list = unAvailableActivityList.stream().map(this::queryStore_list)
				.collect(Collectors.toList());
		storeList.setCant_use_activity_list(un_activity_list);
		ActivityStoreListResponseDto responseDto = new ActivityStoreListResponseDto();
		responseDto.covert(storeList);
		return JSON.toJSONString(responseDto);
	}

	private Activity_Store_list queryStore_list(Object object) {
		if (object == null) {
			return null;
		}
		Activity_Store_list activity_store_list = new Activity_Store_list();
		activity_store_list.covert((JSONObject) object);
		return activity_store_list;
	}

	public String acitivty_coupon_list(ActivityCouponListParameterDto activityCouponListParameterDto) {
		String resultJson = marketingFeignService.getUserCoupon(activityCouponListParameterDto.covert());
		log.info("acitivty_store_list_resultJson:{}", resultJson);
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultData(jsonData);
		log.info("acitivty_store_list_data:{}", data);
		JSONObject json = JSONObject.parseObject(data);
		ActivityCouponList couponList = new ActivityCouponList();
		// 可用优惠活动
		JSONArray availableActivityList = json.getJSONArray("availableCouponList");
		List<Activity_Coupon_list> activity_list = availableActivityList.stream().map(this::queryCoupon_list)
				.collect(Collectors.toList());
		couponList.setCoupon_list(activity_list);
		couponList.setCoupon_count(0);
		if (!CollectionUtils.isEmpty(activity_list)) {
			couponList.setCoupon_count(activity_list.size());
		}
		// 不可用优惠活动
		JSONArray unAvailableActivityList = json.getJSONArray("unavailableCouponList");
		List<Activity_Coupon_list> un_activity_list = unAvailableActivityList.stream().map(this::queryCoupon_list)
				.collect(Collectors.toList());
		couponList.setCant_use_coupon_list(un_activity_list);

		ActivityCouponListResponseDto responseDto = new ActivityCouponListResponseDto();
		responseDto.covert(couponList);
		return JSON.toJSONString(responseDto);
	}

	private Activity_Coupon_list queryCoupon_list(Object object) {
		if (object == null) {
			return null;
		}
		Activity_Coupon_list activity_coupon_list = new Activity_Coupon_list();
		activity_coupon_list.covert((JSONObject) object);
		return activity_coupon_list;
	}

	public String acitivty_card_list(ActivityCardListParameterDto activityCardListParameterDto) {
		String resultJson = marketingFeignService.getUserCard(activityCardListParameterDto.covert());
		log.info("acitivty_store_list_resultJson:{}", resultJson);
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultData(jsonData);
		log.info("acitivty_store_list_data:{}", data);
		JSONObject json = JSONObject.parseObject(data);
		ActivityCardList cardList = new ActivityCardList();
		// 可用优惠活动
		JSONArray availableActivityList = json.getJSONArray("availableCardList");
		List<Activity_Card_list> activity_list = availableActivityList.stream().map(this::queryCard_list)
				.collect(Collectors.toList());
		cardList.setCard_list(activity_list);
		cardList.setCard_count(0);
		if (!CollectionUtils.isEmpty(activity_list)) {
			cardList.setCard_count(activity_list.size());
		}
		// 不可用优惠活动
		JSONArray unAvailableActivityList = json.getJSONArray("unavailableCardList");
		List<Activity_Card_list> un_activity_list = unAvailableActivityList.stream().map(this::queryCard_list)
				.collect(Collectors.toList());
		cardList.setCant_use_card_list(un_activity_list);
		ActivityCardListResponseDto responseDto = new ActivityCardListResponseDto();
		responseDto.covert(cardList);
		return JSON.toJSONString(responseDto);
	}

	private Activity_Card_list queryCard_list(Object object) {
		if (object == null) {
			return null;
		}
		Activity_Card_list activity_card_list = new Activity_Card_list();
		activity_card_list.covert((JSONObject) object);
		return activity_card_list;
	}

	private String getRrovideResultData(JSONObject jsonData) {
		String status = jsonData.getOrDefault("status", "0") == null ? "0" : jsonData.getOrDefault("status", "0").toString();
		if ("1".equals(status)) {
			return jsonData.getOrDefault("data", "") == null ? "" : jsonData.getOrDefault("data", "").toString();
		}
		String msg = jsonData.getOrDefault("msg", "")==null?"":jsonData.getOrDefault("msg", "").toString();
		throw new DataException(DataException.Type.DEFAULT, msg);
	}

}
